package com.example.zsfinacialmanage.view;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.constant.MessageConstant;
import com.example.zsfinacialmanage.utils.MyDBHelper;
import com.example.zsfinacialmanage.utils.Md5Util;
import com.example.zsfinacialmanage.utils.UserExistQuery;
import org.apache.commons.lang3.RandomStringUtils;

public class RegisterActivity extends AppCompatActivity {

    //定义对象
    EditText et_name,et_pwd,et_email,et_phone,et_yzm;
    String pwd_md5,st_name,yzm;
    Button btn_register,btn_cancel;
    TextView txt_yzm;
    MyDBHelper myDBHelper;//创建一个数据库类的文件
    SQLiteDatabase db;//创建一个操作数据库的对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //绑定控件
        initView();
        //注册按钮功能实现
        btnRegister();
        //取消按钮功能实现
        btnCancel();
        //设置验证码
        setYzm();
    }

    private void setYzm() {
        yzm = RandomStringUtils.randomAlphanumeric(6);
        txt_yzm.setText(yzm);
    }

    private void btnCancel() {
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void btnRegister() {
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String useryzm = et_yzm.getText().toString();
                if (useryzm.equalsIgnoreCase(yzm)){
                    //用户名转换位为string类型
                    st_name = et_name.getText().toString();
                    //创建一个对象来封装一行数据
                    ContentValues values = new ContentValues();
                    //将输入的用户名放到name列
                    values.put("name",st_name);
                    // TODO 将密码进行md5加密
                    pwd_md5 = Md5Util.get(et_pwd.getText().toString());
                    //将输入的密码放到pwd列
                    values.put("pwd",pwd_md5);
                    //将输入的邮箱放到email列
                    values.put("email",et_email.getText().toString());
                    //将输入的电话放到phone列
                    values.put("phone",et_phone.getText().toString());
                    //查询用户是否存在
                    if (UserExistQuery.isUserExists(st_name,db)){
                        //弹窗提示"用户存在"
                        Toast.makeText(RegisterActivity.this,st_name + MessageConstant.ALREADY_EXISTS,Toast.LENGTH_SHORT).show();
                        et_name.setText("");
                        et_pwd.setText("");
                        et_phone.setText("");
                        et_email.setText("");
                    }else {
                        //将封装好的这一行数据保存到数据库中的“tb_userinfo”表中
                        db.insert("tb_userinfo", null, values);
                        //弹窗提示
                        Toast.makeText(RegisterActivity.this, MessageConstant.REGISTRATION_SUCCESS, Toast.LENGTH_SHORT).show();
                        //注册成功后返回登录页面
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }else {
                    setYzm();
                    Toast.makeText(RegisterActivity.this,"验证码错误请重新输入！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        et_name = findViewById(R.id.et_name_rg);
        et_pwd = findViewById(R.id.et_pwd_rg);
        et_email = findViewById(R.id.et_email_rg);
        et_phone = findViewById(R.id.et_phone_rg);
        et_yzm = findViewById(R.id.et_yzm_rg);
        btn_register =  findViewById(R.id.bt_ok_rg);
        btn_cancel =  findViewById(R.id.bt_cancel_rg);
        txt_yzm = findViewById(R.id.txt_yzm_rg);
        myDBHelper = new MyDBHelper(RegisterActivity.this);
        db = myDBHelper.getWritableDatabase();
    }
}